\ Tom Dixon
\ 10/1/2008

include FCOMbase.bld
include ACTIVEX
also FCOM

\ Type library for PDF Control
1 0 typelib {05BFD3F1-6319-4F30-B752-C7A22889BCC4}


IAcroAXDocShim comiface pdf

create pdfbuf 256 allot \ temporary buffer

: pdf-close ( -- ) pdf freeref ;

: pdf-open ( str len -- flag ) \ returns true if successful
   >unicode drop pdfbuf swap pdf LoadFile drop pdfbuf @ ; 

: pdf-page! ( n -- ) \ set the current page
   pdf SetCurrentPage drop ;
   
: pdf-firstpage ( -- ) \ goto first page
   pdf GotoFirstPage drop ;
   
: pdf-lastpage ( -- ) \ goto last page
   pdf GotoLastPage drop ;
   
: pdf-nextpage ( -- ) \ goto next page
   pdf GotoNextPage drop ;
   
: pdf-prevpage ( -- ) \ goto previous page
   pdf GotoPreviousPage drop ;
   
: pdf-print ( -- ) \ print the document
   pdf Print drop ;
   
: pdf-printDialog ( -- ) \ prompt with print dialog
   pdf PrintWithDialog drop ;
   
: pdf-printRange ( from to -- ) \ print a range of pages
   pdf PrintPages drop ;
   
: pdf-showscrollbars! ( flag -- ) \ choose to show the scrollbar or not
   pdf SetScrollShowbars drop ;

: pdf-showtoolbar! ( flag -- ) \ choose to show the toolbar or not
   pdf SetShowToolbar drop ;

\ =====================================================
\ Define a Window resource Template for the Main Window
\ =====================================================

NULL value hChild
NULL value hWindow

NextID: IDW_MAINWINDOW

IDW_MAINWINDOW WINDOW 0, 0, 800, 600
BEGIN
   Caption         "ActiveX Window"
   Style           WS_POPUP | WS_BORDER | WS_SYSMENU |
                   WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX |
                   WS_MAXIMIZEBOX | WS_CLIPSIBLINGS | WS_VISIBLE
   ClassStyle      CS_HREDRAW | CS_VREDRAW
   Icon            IDI_APPLICATION
   Cursor          IDC_ARROW
   Brush           WHITE_BRUSH
   Menu            NULL
   WndProc         TestWindowProc
END

: Resize-Child ( lparam -- )
  hChild 0 rot 0 swap
  dup $FFFF and swap 16 rshift $FFFF and
  1 MoveWindow drop ;


\ =====================================================
\ Child window that will host the ActiveX control
\ =====================================================

NextID: IDW_Child

IDW_Child WINDOW    0, 0, 800, 570
BEGIN
        Caption     "ActiveX child window"
        Style       WS_VISIBLE | WS_THICKFRAME | CCS_NORESIZE
        ClassStyle  CS_HREDRAW | CS_VREDRAW
        Icon        IDI_APPLICATION
        Cursor      IDC_ARROW
        Brush       WHITE_BRUSH
        Menu        NULL
        WndProc     ChildWindowProc
END

: ChildWindowProc       { hwnd mesg wparam lparam -- ior }
  case mesg
    WM_CREATE of  hwnd -> hChild  1  endof      \ 1 so that default is performed
    WM_DESTROY of  0 -> hChild  1  endof        \ 1 so that default is performed
      drop  hwnd mesg wparam lparam DefWindowProc
  end-case
;

: RunChild      \ --
  hChild 0= if
    IDW_Child hWindow RESOURCE::CreateChildWindow -> hChild
    AcroPDF CLSID>Str >ascii hChild AXCreate
    pdf freeref
    pdf IAcroAXDocShim hChild AXQuery
  endif
;

: CloseChild    \ --
  hChild if
    pdf-close  hChild SendClose
  endif
;

: TestWindowProc        \ hWnd uMessage wParam lParam -- res
  { hWnd uMessage wParam lParam -- res }
  uMessage case
    WM_SIZE of  lParam Resize-Child  0  endof
    WM_CREATE of  hWnd -> hWindow  1  endof
    WM_DESTROY of  0 -> hWindow  1  endof
      drop  hWnd uMessage wParam lParam DefWindowProc
  end-case
;

: Run ( -- )
  hWindow 0= if
    IDW_MAINWINDOW Resource::CreateParentWindow -> hWindow
    RunChild
  endif
;

: Close ( -- )
  hWindow if
    CloseChild  hWindow SendClose
  endif ;

free-lasttypelib
previous

Run


\ Open an actual file
s" doc\VfxMan.pdf" pdf-open drop
